package pt.inescid.components.policyengine.heimdall.contact;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import pt.inescid.components.policyengine.heimdall.commons.Log;
import pt.inescid.components.policyengine.heimdall.core.HeimdallServer;

public class InboundThread extends Thread {
	
    private Socket socket = null;


    
    /* ===========================================================
	==============================================================
	=============                    =============================
	=============      constructor       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
    public InboundThread(Socket socket) {
    	super("InboundThread");
    	this.socket = socket;
    }
    
    
    
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      registerCommand       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
    public static void registerCommand(RemoteCommand command) {
		
    	// check if it is a response or a request 
		if (command.getType() == RemoteCommand.RESPONSE_COMMAND) {
    		
			HeimdallServer.getInstance().registerNewInfoNotification(
					command);
		}
		else {
			HeimdallServer.getInstance().registerRemoteCommandRequest(
					command);
		}
    }   

    
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      run       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
    public void run() {
		Log.debug(Log.INFO, "Arrived.", "InboundThread.run");
   	
    	try {
    		ObjectOutputStream objectOut = new ObjectOutputStream(
    				socket.getOutputStream());
    		ObjectInputStream objectIn = new ObjectInputStream(
    						socket.getInputStream());
    		
    		RemoteCommand command = (RemoteCommand) objectIn.readObject();

    		Log.debug(Log.INFO, "Received Command: " +
    				command, "InboundThread.run");

    		
    		registerCommand(command);
    		
    		// Confirm Receival
			RemoteResponse response = new RemoteResponse(
					RemoteResponse.OK_RESPONSE, null);
			objectOut.writeObject(response);
   		
    		Log.debug(Log.INFO, "Sent OK response", 
    				"InboundThread.run");

     		// end communication
    		objectOut.close();
    		objectIn.close();
    		socket.close();

    		Log.debug(Log.INFO, "Finished.", "InboundThread.run");

    	} catch (IOException e) {
			Log.debug(Log.ERROR, "Error in communication: " +
					e.getMessage(), "InboundThread.run");
    	} catch (ClassNotFoundException e) {
			Log.debug(Log.ERROR, "Error in communication: " +
					e.getMessage(), "InboundThread.run");
    	}
    }
}; // InboundThread
